
layui.use(['layer'], async function () {
    function getCurrentDateTime() {
        var currentDate = new Date();
        
        // 获取月份和日期，并确保显示为两位数
        var month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
        var date = currentDate.getDate().toString().padStart(2, '0');
        
        // 获取小时，并确保显示为两位数
        var hours = currentDate.getHours().toString().padStart(2, '0');
        
        // 构建格式化后的时间字符串
        var formattedDateTime = month + '-' + date + ' ' + hours + ':00';
        
        return formattedDateTime;
    }
    var layer = layui.layer;

    var chatArea = document.getElementById('chatArea');
    var messageInput = document.getElementById('messageInput');
    var sendBtn = document.getElementById('sendBtn');

    sendBtn.addEventListener('click', async function () {
        var message = messageInput.value;
        if (message.trim() !== '') {
            var loadingIndex = layer.load(2,{time:0});
            response=await taskObject.fetchtemplate(path='/api/user/chat',method='POST',data={'message':message})
            sendMessage('我', message,getCurrentDateTime());
            sendMessage('小智',response.message,getCurrentDateTime())
            layer.close(loadingIndex)
            messageInput.value = '';
        }
    });

    function sendMessage(username, message,timestamp) {
        var messageContainer = document.createElement('div');
        var messageBubble = document.createElement('div');
        var usernameSpan = document.createElement('span');
        var timestampSpan = document.createElement('span');

        messageContainer.classList.add('message');

        if (username === '我') {
            messageContainer.classList.add('self');
        } else {
            messageContainer.classList.add('other');
        }

        usernameSpan.textContent = username;
        usernameSpan.classList.add('username');

        timestampSpan.textContent = getCurrentTimestamp(timestamp);
        timestampSpan.classList.add('timestamp');

        messageBubble.textContent = message;
        messageBubble.classList.add('message-bubble');

        messageContainer.appendChild(usernameSpan);
        messageContainer.appendChild(timestampSpan);
        messageContainer.appendChild(document.createElement('br'));
        messageContainer.appendChild(messageBubble);

        chatArea.appendChild(messageContainer);
        chatArea.scrollTop = chatArea.scrollHeight;
    }

    function getCurrentTimestamp(timestamp) {
        
        return timestamp
    }
    response=await taskObject.fetchtemplate(path='/api/user/chat')
    for (key of response.messagedata){
        if(key.role=='user'){
            sendMessage('我',key.content,key.timestamp)
        }else{
            sendMessage('小智',key.content,key.timestamp)
        }
       
    }
});


layui.use(['form','jquery'], function(){
    let form=layui.form;
    let $=layui.$;
    let coursehtml=''
   !(async function(){
   let response=await taskObject.fetchtemplate(path='/api/user/course')
    console.log(response);
   
   response.data.forEach((key)=>{
        
        coursehtml+=`<option value="${key.courseid}">${key.coursename}</option>`
    })
    $('#courseId').html(coursehtml)
   form.render()
   }())
   
    
    form.on('submit(sendBtn)',function(data){
        if (data.field.courseid==''){
            return false;
        }
        !(async function(){
            
            response=await taskObject.fetchtemplate(path='/api/user/uniquevalue',method='POST',data={'courseid':data.field.courseid})
            
        }())
        return false
    })
    
})